//
//  YCSignUpLocViewController.m
//  YCSchool
//
//  Created by yanglinxia on 16/4/29.
//  Copyright © 2016年 yanglinxia. All rights reserved.
//  报名点

#import "YCSignUpLocViewController.h"
#import "YCSignUpMapViewController.h"
#import "YCSignUpLocCell.h"
#import "YCSignUpLocModel.h"

@interface YCSignUpLocViewController ()<UITableViewDelegate,UITableViewDataSource>
/** tableView */
@property (nonatomic, strong) UITableView *tableView;
/** 数据  */
@property (nonatomic, strong) NSArray *dataArray;
@end

@implementation YCSignUpLocViewController

- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.yc_width, self.view.yc_height-64) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.tableFooterView = [[UIView alloc] init];
        UINib *nib = [UINib nibWithNibName:NSStringFromClass([YCSignUpLocCell class]) bundle:nil];
        [_tableView registerNib:nib forCellReuseIdentifier:@"signUpLocCell"];
    }
    return _tableView;
}

- (NSArray *)dataArray
{
    if (!_dataArray) {
        YCSignUpLocModel *model = [[YCSignUpLocModel alloc] init];
        model.icon = @"http://pic10.nipic.com/20101020/3650425_202918301404_2.jpg";
        model.address = @"一乘驾校(金殿点)";
        model.detailAddress = @"集中式教学培训";
        model.loc = NO;
        
        YCSignUpLocModel *model1 = [[YCSignUpLocModel alloc] init];
        model1.icon = @"http://pic6.nipic.com/20100323/4214896_084058018310_2.jpg";
        model1.address = @"一乘驾校(广州点)";
        model1.detailAddress = @"广东省广州市天河区金穗路3号汇美大厦1603广州新关系科技有限公司";
        model1.loc = NO;
        
        YCSignUpLocModel *model2 = [[YCSignUpLocModel alloc] init];
        model2.icon = @"http://pica.nipic.com/2007-10-17/20071017111345564_2.jpg";
        model2.address = @"一乘驾校(金殿点)";
        model2.detailAddress = @"集中式教学培训";
        model2.loc = NO;
        
        _dataArray = @[model,model1,model2];
    }
    return _dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"报名点";
    [self yc_addBackBarButtonItem];
    [self.view addSubview:self.tableView];
    self.tableView.contentInset = UIEdgeInsetsMake(5, 0, 0, 0);
    self.tableView.rowHeight = 91;//63+28
    
    //下拉刷新
    YCWeakSelf;
    self.tableView.mj_header = [YCRefreshHeader headerWithRefreshingBlock:^{
        [weakSelf loadNewData];
    }];
    
    self.tableView.mj_footer = [YCRefreshFooter footerWithRefreshingBlock:^{
        [weakSelf loadMoreData];
    }];
    [self.tableView.mj_header beginRefreshing];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - 数据处理相关
- (void)loadNewData
{
    // 1.添加数据
    
    // 模拟2秒后刷新表格UI
    YCWeakSelf;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

        [weakSelf.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
    });
}

// 上拉加载
- (void)loadMoreData
{
    // 添加数据
    BOOL flag = YES;//用来判断是不是还有数据
    
    // 模拟2秒后刷新表格UI
    YCWeakSelf
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 刷新表格
        [weakSelf.tableView reloadData];
        
        if (flag) {
            [weakSelf.tableView.mj_footer endRefreshing];//结束上拉刷新
        } else {
            [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];//没有更多数据可加载
        }
        //        weakSelf.tableView.mj_footer.hidden = YES;//只加载一次,只要hiden就可以了
    });
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YCSignUpLocCell *cell = [tableView dequeueReusableCellWithIdentifier:@"signUpLocCell"];
    cell.contentView.backgroundColor = [UIColor whiteColor];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.itemModel = self.dataArray[indexPath.row];
    return cell;
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    YCLogFunc
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    YCSignUpMapViewController *mapVc = [[YCSignUpMapViewController alloc] init];
    [self.navigationController pushViewController:mapVc animated:YES];
}


#pragma mark - 点击事件






@end